package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcPatrolConfig;
import com.csun.cmny.provider.model.domain.DmcPatrolGreatConfig;
import com.csun.cmny.provider.model.domain.DmcPatrolGreatLocation;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.patrol.PatrolBindTaskDto;
import com.csun.cmny.provider.model.dto.patrol.PatrolPageQuery;
import com.csun.cmny.provider.model.dto.patrol.PatrolQueryDto;
import com.csun.cmny.provider.model.dto.patrol.PatrolTaskDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.validator.DmcDeviceEditValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Auther: HD
 * @Date: 2021-07-26
 * @Description: the class dmc device patrol controller.
 */
@RestController
@RequestMapping(value = "/patrol", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcPatrolController extends BaseController {

    @Resource
    private DmcPatrolGreatLocationService dmcPatrolGreatLocationServiceImp;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcPatrolGreatConfigService dmcPatrolGreatConfigServiceImp;
    @Resource
    private DmcPatrolRecordService dmcPatrolRecordService;
    @Resource
    private DmcPatrolReminderService dmcPatrolReminderServiceImp;

    /*
    * 巡更棒的分页查询
    * */
    @RequestMapping(value = "/queryGroupPatrolListWithPage", method = RequestMethod.POST)
    public Wrapper queryGroupPatrolListWithPage(@RequestBody PatrolPageQuery patrolPageQuery){
        patrolPageQuery.setGroupId(getLoginAuthDto().getGroupId());
        patrolPageQuery.setDeviceType(DmcDeviceTypeEnum.PATROL.getIndex());
        PageInfo pageInfo = dmcPatrolGreatLocationServiceImp.queryDeviceListWithPage(patrolPageQuery);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }
    /*
     * 巡更棒的修改
     * */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper updatePatrol(@Validated(value = {DmcDeviceEditValidator.class})
                                    @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br){
        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.PATROL.getIndex());
        Integer result = dmcDeviceService.editDevice(dmcDevice, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    /*
     * 绑定巡更棒
     * */
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Wrapper patrolBind(@RequestBody PatrolQueryDto patrolQueryDto){
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        patrolQueryDto.setGroupId(loginAuthDto.getGroupId());

        DmcPatrolGreatLocation dmcPatrolGreatLocation = new DmcPatrolGreatLocation();
        BeanUtils.copyProperties(patrolQueryDto, dmcPatrolGreatLocation);
        dmcPatrolGreatLocation.setCreator(patrolQueryDto.getUserName());
        dmcPatrolGreatLocation.setCreatorId(patrolQueryDto.getUserId());
        //查询用户是否绑定
        List<DmcPatrolGreatLocation> dgl = dmcPatrolGreatLocationServiceImp.selectByUserId(patrolQueryDto.getUserId());
        if(dgl.size()!=0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该用户已绑定");
        }
        //查询设备是否绑定
        DmcPatrolGreatLocation greatLocation = dmcPatrolGreatLocationServiceImp.selectByKey(dmcPatrolGreatLocation.getDeviceId());
        if(greatLocation!=null){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        Integer result = dmcPatrolGreatLocationServiceImp.patrolBind(dmcPatrolGreatLocation);
        if (result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    /*
     * 解绑巡更棒
     * */
    @RequestMapping(value = "/untied", method = RequestMethod.POST)
    public Wrapper patrolUntied(@RequestBody PatrolQueryDto patrolQueryDto){

        DmcPatrolGreatLocation dmcPatrolGreatLocation = new DmcPatrolGreatLocation();
        BeanUtils.copyProperties(patrolQueryDto, dmcPatrolGreatLocation);
        //解绑巡更棒
        int result = dmcPatrolGreatLocationServiceImp.delete(dmcPatrolGreatLocation);
        //根据巡更棒id删除巡更记录
        dmcPatrolRecordService.deleteByPatrolId(dmcPatrolGreatLocation.getDeviceId());
        //解绑删除巡更提醒
        dmcPatrolReminderServiceImp.deleteByPatrolId(dmcPatrolGreatLocation.getDeviceId());
        if(result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    /*
    * 查询未绑定巡更点的巡更棒
    * */
    @RequestMapping(value = "/selectUntiedPatrol",method = RequestMethod.POST)
    public Wrapper selectUntiedPatrol(@RequestBody PatrolQueryDto patrolQueryDto){
        PatrolPageQuery patrolPageQuery = new PatrolPageQuery();
        patrolPageQuery.setGroupId(getLoginAuthDto().getGroupId());
        patrolPageQuery.setDeviceType(DmcDeviceTypeEnum.PATROL.getIndex());
        patrolPageQuery.setDeviceId(patrolQueryDto.getId());
        patrolPageQuery.setPageNum(null);
        patrolPageQuery.setPageSize(null);
        List<PatrolPageQuery> patrolPageQuerys =  dmcPatrolGreatLocationServiceImp.selectUntiedPatrol(patrolPageQuery);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),patrolPageQuerys);
    }
    /*
    * 查询已绑定巡更点的巡更棒
    * */
    @RequestMapping(value = "/selectBindPatrol",method = RequestMethod.POST)
    public Wrapper selectBindPatrol(@RequestBody PatrolQueryDto patrolQueryDto){
        PatrolPageQuery patrolPageQuery = new PatrolPageQuery();
        patrolPageQuery.setGroupId(getLoginAuthDto().getGroupId());
        patrolPageQuery.setDeviceType(DmcDeviceTypeEnum.PATROL.getIndex());
        patrolPageQuery.setDeviceId(patrolQueryDto.getId());
        patrolPageQuery.setPageNum(null);
        patrolPageQuery.setPageSize(null);
        List<PatrolPageQuery> patrolPageQueries = dmcPatrolGreatLocationServiceImp.selectBindPatrol(patrolPageQuery);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),patrolPageQueries);
    }


    /*
    * 新增巡更任务
    * */
    @RequestMapping(value = "/patrolAddTask",method = RequestMethod.POST)
    public Wrapper patrolAddTask(@RequestBody PatrolTaskDto patrolTaskDto){
        DmcPatrolGreatConfig dmcPatrolConfig = new DmcPatrolGreatConfig();
        BeanUtils.copyProperties(patrolTaskDto,dmcPatrolConfig);
        dmcPatrolConfig.setCreator(getLoginAuthDto().getUserName());
        dmcPatrolConfig.setCreatorId(getLoginAuthDto().getUserId());
         Integer result = dmcPatrolGreatConfigServiceImp.patrolTaskAdd(dmcPatrolConfig);
        if (result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    /*
    * 删除巡更任务
    * */
    @RequestMapping(value = "/patrolDeleteTask",method = RequestMethod.POST)
    public Wrapper patrolDeleteTask(@RequestBody PatrolTaskDto patrolTaskDto){
        Integer result = dmcPatrolGreatConfigServiceImp.deleteByKey(patrolTaskDto.getId());
        if (result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    /*
    * 修改巡更任务
    * */
    @RequestMapping(value = "/patrolUpdateTask",method = RequestMethod.POST)
    public Wrapper patrolUpdateTask(@RequestBody PatrolTaskDto patrolTaskDto){
        DmcPatrolGreatConfig dmcPatrolConfig = new DmcPatrolGreatConfig();
        BeanUtils.copyProperties(patrolTaskDto,dmcPatrolConfig);
        dmcPatrolConfig.setLastOperator(getLoginAuthDto().getUserName());
        dmcPatrolConfig.setLastOperatorId(getLoginAuthDto().getUserId());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(new Date());
        try{
            Date date = df.parse(format);
            dmcPatrolConfig.setUpdateTime(date);
        }catch (Exception e){}
        Integer result = dmcPatrolGreatConfigServiceImp.update(dmcPatrolConfig);
        if (result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*
     * 查询巡更任务
     * */
    @RequestMapping(value = "/patrolSelectTask",method = RequestMethod.POST)
    public Wrapper patrolSelectTask(@RequestBody PatrolTaskDto patrolTaskDto){
        patrolTaskDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = dmcPatrolGreatConfigServiceImp.patrolSelectTask(patrolTaskDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }
}
